package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.验证参数是否合法
        if (dto == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        dto.checkParam();
        IPage iPage = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdChannel> adChannelLambdaQueryWrapper = null;
        if (dto.getName() != null) {
            adChannelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            adChannelLambdaQueryWrapper.like(AdChannel::getName, dto.getName());
        }
        IPage page = page(iPage, adChannelLambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }


    @Override
    public ResponseResult insertAdChannel(AdChannel channel) {
        if (channel == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        channel.setCreatedTime(new Date());
        save(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult updateAdChannel(AdChannel channel) {
        if (channel == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        if (getById(channel.getId()) == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        UpdateWrapper<AdChannel> wrapper = new UpdateWrapper<>();
        wrapper.in("id", channel.getId());
        update(channel, wrapper);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteAdChannel(Integer id) {
        if (id == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        AdChannel byId = getById(id);
        if (byId == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
